package com.spica.platform.feign.config;

import com.spica.platform.base.context.TenantContextHolder;
import com.spica.platform.common.constant.SecurityConstant;
import com.spica.platform.common.utils.StringUtil;
import feign.RequestInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
 * feign拦截器，只包含基础数据
 */
public class FeignBaseInterceptorConfig {
    /**
     * 使用feign client访问别的微服务时，将上游传过来的client等信息放入header传递给下一个服务
     */
    @ConditionalOnMissingBean(RequestInterceptor.class)
    @Bean
    public RequestInterceptor baseFeignInterceptor() {
        return template -> {
            //传递client
            String tenant = TenantContextHolder.getTenant();
            if (StringUtil.isNotEmpty(tenant)) {
                template.header(SecurityConstant.TENANT_HEADER, tenant);
            }
        };
    }
}
